﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace NHINDirect
{
    public class NHINDMessage : Message
    {
        NHINDAgent m_agent;
        NHINDAddress m_sender;
        
        internal NHINDMessage()
        {
        }
        
        public NHINDAgent Agent
        {
            get
            {
                return this.m_agent;
            }
            internal set
            {
                this.m_agent = value;
            }
        }
                
        public override Header From
        {
            get
            {
                return base.From;
            }
            set
            {
                base.From = value;
                this.m_sender = null;
            }
        }
        
        public NHINDAddress Sender
        {
            get
            {
                if (this.m_sender == null)
                {
                    this.m_sender = this.ToNHINDAddress(this.From);
                }
                
                return this.m_sender;
            }
        }
        
        public virtual void Validate()
        {
            if (this.ParsedTo == null)
            {
                throw new ProtocolException(ProtocolError.MissingTo);
            }

            if (this.ParsedFrom == null)
            {
                throw new ProtocolException(ProtocolError.MissingFrom);
            }
        }        
        
        NHINDAddress ToNHINDAddress(Header header)
        {
            if (header == null)
            {
                return null;
            }

            return new NHINDAddress(header.Value);
        }
    }    
}
